package lab_2_server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import lab_2_server.handlers.ClientHandler;
import lab_2_server.handlers.ServerHandler;

/**
 * GUI frame
 * @author pacific
 */
public class ServerMonitorUI extends JFrame {
    
    private static final String LABEL_PREFIX = "<html><h3><font color=green>" ;

    public ServerMonitorUI() {
        setLayout(new BorderLayout());
        
        add( buildHeaderPanel(), BorderLayout.NORTH ) ;

        JPanel panelForLists = new JPanel(new GridLayout(1, 2)) ;
        add(panelForLists, BorderLayout.CENTER) ;

        //  add panel for clientsList with clients
        panelForLists.add( buildPanelForClients() ) ;

        //  add panel for clientsList with servers        
        panelForLists.add( buildPanelForServers() ) ;

        // add button "EXIT"
        JButton exit = new JButton("Завершить работу сервера") ;
        exit.addActionListener(new ExitListener()) ;
        add(exit, BorderLayout.SOUTH) ;

        setBounds(300, 300, 300, 300) ;
        setResizable(false) ;
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE) ;
        setVisible(true);
    }
    

    /** Create panel for header */
    private JPanel buildHeaderPanel() {
        JPanel headerPanel = new JPanel(new FlowLayout());
        headerPanel.add(getHeaderLabel());
        
        return headerPanel ;
    }

    /** Create panel for client list */
    private JPanel buildPanelForClients() {
        JPanel forClients = new JPanel(new BorderLayout());
        JLabel label = new JLabel(LABEL_PREFIX + "Список клиентов:", JLabel.CENTER);
        label.setBackground(Color.BLACK);
        label.setOpaque(true);
        forClients.add(label, BorderLayout.NORTH);
        JList clientsList = new JList(getClientsModel());
        clientsList.setBackground(Color.BLACK);
        clientsList.setOpaque(true);
        forClients.add(clientsList, BorderLayout.CENTER);
        return forClients ;
    }


    /** Create panel for server list */
    private JPanel buildPanelForServers() {
        JPanel forServers = new JPanel(new BorderLayout());
        JLabel label = new JLabel(LABEL_PREFIX + "Список серверов:", JLabel.CENTER);
        label.setBackground(Color.BLACK);
        label.setOpaque(true);
        forServers.add(label, BorderLayout.NORTH);
        JList serversList = new JList(getServersModel());
        serversList.setBackground(Color.BLACK);
        serversList.setOpaque(true);
        forServers.add(serversList, BorderLayout.CENTER);
        return forServers ;
    }


    /** Add client to UI */
    private final static String prefix =  "<HTML><FONT COLOR=GREEN>" ;
    public void addClient( ClientHandler handler ) {
        MyLogger.putMessange("Add client to UI") ;
        getClientsModel().addElement( prefix + handler.toString() );
    }


    /** Delete client from UI */
    public void delClient( ClientHandler handler ) {
        MyLogger.putMessange("Delete client from UI") ;
        getClientsModel().removeElement( prefix + handler.toString() );
        
        repaint() ;
    }

    /** Add server to UI */
    public void addServer( ServerHandler handler ) {
        MyLogger.putMessange("Add server to UI") ;
        getServersModel().addElement( prefix + handler.toString() );
    }


    /** Delete server from UI */
    public void delServer( ServerHandler handler ) {
        MyLogger.putMessange("Delete Server from UI") ;
        getServersModel().removeElement( prefix + handler.toString() );
    }


    /** Main method*/
    public static void main(String args[]) {
        ServerMonitorUI monitor = new ServerMonitorUI() ;
        Server serv = new Server(monitor) ;
        monitor.setServer( serv ) ;
        serv.start() ;
    }


    /**Listener for button "Close server"*/
    class ExitListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if( getServer() != null )
                getServer().exit() ;
            System.exit(0) ;
        }

    }


    //-----Fields-----

    private final JLabel header = new JLabel("Сервер", JLabel.CENTER) ;
    private DefaultListModel clientsModel = new DefaultListModel() ;
    private DefaultListModel serversModel = new DefaultListModel() ;
    //Reference to server's thread
    Server server ;


    //-----GETTERS and SETTERS-----

    private DefaultListModel getClientsModel() {
        return clientsModel;
    }
    private DefaultListModel getServersModel() {
        return serversModel;
    }

    private Server getServer() {
        return server;
    }

    private void setServer(Server server) {
        this.server = server;
    }

    private JLabel getHeaderLabel() {
        return header;
    }
}